<template>
  <div class="carousel">
    <page-header title="首页轮播图配置">
      <page-search><el-button @click="showDialog()">添加轮播图</el-button> </page-search>
    </page-header>
    <el-table
      :data="tableData"
      :max-height="clientHeight - 250"
      :cell-class-name="setCellClassName">
      <el-table-column prop="imgUrl" label="轮播图">
        <template slot-scope="{ row }">
          <el-image
            style="width: 70px; height: 46px"
            :src="row.imgUrl"
            :preview-src-list="carouselList">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="courseName" label="链接课程" />
      <el-table-column
        label="操作"
        min-width="100px"
      >
        <template slot-scope="{ row }">
          <action-text @click="showDialog(row)">修改</action-text>
          <action-text @click="del(row)" type="danger">删除</action-text>
        </template>
      </el-table-column>
    </el-table>
    <page-pagination
      :pageSize="pageSize"
      :currentPage="pageNum"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <formDialog
      ref="dialog"
      :initFormData="formData"
      :isCreate="isCreate"
      @confirm="getData"
    />
  </div>
</template>
<script>
import paginationMixin from '@/mixins/pagination';
import { getBannerList, deleteBanner } from '@/api/base-config';
import formDialog from './dialog';

const initFormData = {
  imgUrl: '',
  courseId: '',
};

export default {
  data() {
    return {
      tableData: [],
      carouselList: [],
      isCreate: false,
      formData: {
        ...initFormData,
      },
    };
  },
  components: {
    formDialog,
  },
  mixins: [paginationMixin],
  methods: {
    setCellClassName({ column }) {
      if (column.property === 'imgUrl') {
        return 'image-cell';
      }
      return '';
    },
    async del(row) {
      this.$confirm('确定删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        await deleteBanner(row.id);
        this.$message.success('删除成功');
        await this.getData();
      }).catch(() => {});
    },
    showDialog(data) {
      if (data) {
        const {
          id, imgUrl, courseId, courseName,
        } = data;
        this.formData = {
          id, imgUrl, courseId, name: courseName,
        };
        this.isCreate = false;
      } else {
        this.formData = { ...initFormData };
        this.isCreate = true;
      }
      this.$refs.dialog.dialogVisible = true;
    },
    async getData() {
      const result = await getBannerList(this.searchParams);
      const { total, list } = result;
      this.total = total;
      this.tableData = list && list.map((item) => {
        const { createTime } = item;
        return {
          ...item,
          createTime: this.$formatTime(createTime),
        };
      });
    },
  },
  async created() {
    await this.getData();
  },
};
</script>
